<template>
  <div class="user-login login-bg">
    <div class="user-login--content">
      <img :src="logo" alt="" />
      <a-form class="form" ref="formRef" :model="formState" :rules="rules" v-bind="layout">
        <a-form-item class="form-item" has-feedback name="uuid">
          <a-input
            class="input"
            v-model:value="formState.uuid"
            type="text"
            @keyup.enter="onSubmit()"
            placeholder="请输入用户名"
            autocomplete="off"
            allow-clear
          />
        </a-form-item>
        <a-form-item class="form-item" has-feedback name="password">
          <a-input
            class="input"
            v-model:value="formState.password"
            type="password"
            @keyup.enter="onSubmit()"
            placeholder="请输入登录密码"
            autocomplete="off"
            allow-clear
          />
        </a-form-item>
        <a-form-item class="form-item" has-feedback name="code">
          <a-row>
            <a-col :span="12">
              <a-input
                class="input"
                v-model:value="formState.code"
                @keyup.enter="onSubmit()"
                placeholder="请输入验证码"
                autocomplete="off"
                width="100px"
                allow-clear
              />
            </a-col>
            <a-col :offset="1" :span="8">
              <Identify :check-code="identifyCode" @click="getCode"/>
            </a-col>
          </a-row>
        </a-form-item>
        <a-button class="login-button mt-24" type="primary" size="large" block @click="onSubmit()">登录</a-button>
      </a-form>
    </div>
  </div>
</template>

<script lang="ts">
  import { message } from 'ant-design-vue'
  import { defineComponent, onMounted, reactive, ref, toRaw, UnwrapRef } from 'vue'
  import { useRoute, useRouter } from 'vue-router'
  import { useStore } from '@/store'
  import logo from '@/assets/image/logo_login.png'
  import Identify from './components/SIdentify.vue'
  import { checkCode } from '@/apis/account'

  export default defineComponent({
    components: {
      Identify
    },
    setup() {
      const formRef = ref()
      const $route = useRoute()
      const $router = useRouter()
      const { userStore } = useStore()
      const identifyCode = ref('')
      const redirect: any = decodeURIComponent(($route.query.redirect as any) || '/welcome')

      const formState: any = reactive({
        uuid: '',
        password: '',
        code: '',
        key: ''
      })

      const getCode = () => {
        identifyCode.value = ''
        checkCode().then((res: any) => {
          if (res.code === 200) {
            identifyCode.value = res.check_code.toString()
            formState.key = res.key
          }
        })
      }

      const rules = {
        uuid: [{ required: true, message: '请输入账号', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        code: [{ required: true, message: '请输入验证码', trigger: 'blur' }]
      }

      const layout = {
        labelCol: { span: 0 },
        wrapperCol: { span: 24 }
      }

      const resetForm = () => {
        formRef.value.resetFields()
      }

      const onSubmit = () => {
        formRef.value.validate().then(() => {
          const params = JSON.parse(JSON.stringify(toRaw(formState)))
          userStore.handleLogin(params).then((res: any) => {
            if (res && res.id) {
              $router.replace({
                path: redirect || 'welcome'
              })
            } else {
              getCode()
            }
          })
        })
      }

      onMounted(() => {
        getCode()
      })

      return {
        redirect,
        formRef,
        formState,
        rules,
        layout,
        logo,
        identifyCode,
        resetForm,
        onSubmit,
        getCode
      }
    }
  })
</script>

<style lang="less" scoped>
  .user-login {
    display: flex;
    flex-direction: column;
    //align-items: center;
    justify-content: center;
    height: 100%;
    background-size: 100% 100%;
    padding-left: 55%;
    background: url('../../assets/image/login_bg.png') no-repeat center center;
    &--content {
      width: 384px;
      height: 360px;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      align-content: center;
      background-color: #fff;
      padding: 40px;
      box-shadow: 0 4px 22px 3px rgba(2, 15, 58, 0.51);
      border-radius: 10px;

      img {
        width: auto;
        height: 50px;
        margin-bottom: 20px;
      }

      .form {
        width: 100%;
      }
    }

    &--footer {
      text-align: center;
      padding: 24px 50px;
      font-size: 12px;

      &.record-number {
        color: rgba(255, 255, 255, 0.75);

        &:hover {
          color: #fff;
        }
      }
    }

    .login-footer {
      color: #ffffff;
      position: fixed;
      width: 100%;
      line-height: 40px;
      left: 0;
      bottom: 0;
      text-align: center;
    }
  }
</style>
